//
//  Clock.m
//  Time
//
//  Created by Phan Ngoc Tien on 1/9/13.
//  Copyright (c) 2013 Phan Ngoc Tien. All rights reserved.
//

#import "Clock.h"

@implementation Clock
{
    __weak NSTimer * timer;
    int _time;
    float startTime;
    UILabel *lbTime;
    int tem,red,green;
    float step;
    UIColor *color;
    UIImage * image;
}
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame Time:(int)time
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _time = time*100;
        tem=0;
        [self setBackgroundColor:[UIColor clearColor]];
        self.opaque=NO;
        self.clearsContextBeforeDrawing=YES;
        red = 0;
        green = 255;
        step = 0;
        image = [UIImage imageNamed:@"clock.png"];
        
    }
    return self;
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 20);
    CGContextClearRect(context, rect);
    //  CGContextDrawImage(context, rect, image.CGImage);
    UIGraphicsBeginImageContext(image.size);
    
    UIGraphicsPushContext(context);
    
    [image drawInRect:rect];
    
    UIGraphicsPopContext();
    
    UIGraphicsEndImageContext();
    if(tem > step)
    {
        if(red == 255 && green>0)
            green--;
        else
            red++;
        color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:0.0 alpha:1.0];
        
        step +=_time / 500.0;
    }
    [color setStroke];
    
    
    CGContextAddArc(context,self.frame.size.height/2,
                    self.frame.size.width/2,
                    50, startTime - 0.5 * M_PI,  1.5*M_PI, 0);
    CGContextStrokePath(context);
    //  [[UIColor blueColor] setStroke];
    CGContextSetLineWidth(context, 1);
    
    CGContextAddArc(context,self.frame.size.height/2,
                    self.frame.size.width/2,
                    40, 0, 2*M_PI, 0);
    CGContextStrokePath(context);
    
    CGContextAddArc(context,self.frame.size.height/2,
                    self.frame.size.width/2,
                    60, 0,  2*M_PI, 0);
    CGContextStrokePath(context);
    
    lbTime= [[UILabel alloc] initWithFrame:CGRectMake(30,80,140,40)];
    //lbTime.font=[UIFont systemFontOfSize:45];
    lbTime.font = [UIFont boldSystemFontOfSize:45];
    if(tem %100 == 0)
    {
        NSArray * array=[self subviews];
        for (UIView*v in array)
        {
            [v removeFromSuperview];
        }
        lbTime.text = [NSString stringWithFormat:@"%d",_time/100 - tem /100];
    }
    if(tem == _time)
    {
        lbTime.font=[UIFont systemFontOfSize:30];
        lbTime.text = @"Time out";
    }
    
    
    lbTime.textColor = [UIColor redColor];
    lbTime.textAlignment = NSTextAlignmentCenter;
    lbTime.backgroundColor=[UIColor clearColor];
    [lbTime removeFromSuperview];
    [self addSubview:lbTime];
}
- (void) settime:(int)time
{
    _time = time*100;
}
- (void) stop
{
    [timer invalidate];
    
}
- (void) changeTime
{
    tem ++;
    startTime = tem * 2 *M_PI / _time;
    [self setNeedsDisplay];
    if( tem % 100 == 0)
        lbTime.text = [NSString stringWithFormat:@"%d",tem/100];
    if(tem == _time)
    {
        [timer invalidate];
        [self setNeedsDisplay];
        [self.delegate isTimeOut:YES];
    }
}
- (void)run
{
    tem = 0;
    [self.delegate isStartCountdown];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];
}


@end
